//
//  ViewController.m
//  GCD常用函数
//
//  Created by 李煜 on 2024/3/20.
//

#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) NSOperationQueue *queue;
@end

@implementation ViewController

-(void)viewDidLoad {
	[super viewDidLoad];
	
	
	
}
- (IBAction)start:(id)sender {
	NSOperationQueue *queue1 = [[NSOperationQueue alloc]init];
	//设置最大并发数=1,线程同步(同一时间只能执行一个任务)
	//默认=-1,表示不限制
	//>1则开线程
	queue1.maxConcurrentOperationCount = 1;
	self.queue = queue1;
	
	NSBlockOperation *op1= [NSBlockOperation blockOperationWithBlock:^{
		for (int i = 0; i <1000; i++) {
			NSLog(@"--down1-%d--%@",i,[NSThread currentThread]);
		}
	}];
	NSBlockOperation *op2= [NSBlockOperation blockOperationWithBlock:^{
		for (int i = 0; i <1000; i++) {
			NSLog(@"--down2-%d--%@",i,[NSThread currentThread]);
		}
	}];
	NSBlockOperation *op3= [NSBlockOperation blockOperationWithBlock:^{
		for (int i = 0; i <1000; i++) {
			NSLog(@"--down3-%d--%@",i,[NSThread currentThread]);
		}
	}];
	NSBlockOperation *op4= [NSBlockOperation blockOperationWithBlock:^{
		for (int i = 0; i <1000; i++) {
			NSLog(@"--down4-%d--%@",i,[NSThread currentThread]);
		}
	}];
	
		//队列1添加op1 op2 op3
	[queue1 addOperation:op1];
	[queue1 addOperation:op2];
	[queue1 addOperation:op3];
	[queue1 addOperation:op4];
	
}
- (IBAction)suspend:(id)sender {
	self.queue.suspended = YES;
}
- (IBAction)resume:(id)sender {
	//队列中的任务是有状态的,||已完成|正在运行|排队等待
	//正在执行的任务无法马上(暂停和取消),后续等待的任务得等当前任务执行完成才可以暂停和取消;
	self.queue.suspended = NO;
}
- (IBAction)cancel:(id)sender {
	[self.queue cancelAllOperations];
}

@end
